Micron Document
`:top
In `F33f`_`[computer programming`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Computer_programming]`_`f, a `!static variable`! is a `F33f`_`[variable`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Variable_(programming)]`_`f that has been `F33f`_`[allocated`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Memory_allocation]`_`f "statically", meaning that its `F33f`_`[lifetime`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Variable_lifetime]`_`f (or "extent") is the entire run of the program. This is in contrast to shorter-lived `F33f`_`[automatic variables`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Automatic_variable]`_`f, whose storage is `F33f`_`[stack allocated`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Stack_allocation]`_`f and deallocated on the `F33f`_`[call stack`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Call_stack]`_`f; and in contrast to `F33f`_`[dynamically allocated`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Dynamic_memory_allocation]`_`f objects, whose storage is allocated and deallocated in `F33f`_`[heap memory`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Heap_memory]`_`f.

`F33f`_`[Variable lifetime`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Variable_lifetime]`_`f is contrasted with `F33f`_`[scope`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Scope_(computer_science)]`_`f (where a variable can be used): "global" and "local" refer to scope, not lifetime, but scope often implies lifetime. In many languages, `F33f`_`[global variables`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Global_variable]`_`f are always static, but in some languages they are dynamic, while `F33f`_`[local variables`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Local_variable]`_`f are generally automatic, but may be static.

In general, `!static memory allocation`! is the allocation of memory at `F33f`_`[compile time`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Compile_time]`_`f, before the associated program is executed, unlike `F33f`_`[dynamic memory allocation`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Dynamic_memory_allocation]`_`f or `F33f`_`[automatic memory allocation`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Automatic_memory_allocation]`_`f where memory is allocated as required at `F33f`_`[run time`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Run_time_(program_lifecycle_phase)]`_`f.`:cite-ref-1[`F5bf`_`[1`#cite-note-1]`_`f]

>>Contents

• `F0af`_`[History`#history]`_`f
• `F0af`_`[Addressing`#addressing]`_`f
• `F0af`_`[Scope`#scope]`_`f
• `F0af`_`[Example`#example]`_`f
• `F0af`_`[Object-oriented programming`#object-oriented-programming]`_`f
• `F0af`_`[See also`#see-also]`_`f
• `F0af`_`[Notes`#notes]`_`f
• `F0af`_`[References`#references]`_`f

-─

>>History

Static variables date at least to `F33f`_`[ALGOL 60`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ALGOL_60]`_`f (1960), where they are known as `*`!own`! variables`*:

`*A declaration may be marked with the additional declarator `!own.`! This has the following effect: upon a re-entry into the block, the values of `!own`! quantities will be unchanged from their values at the last exit, while the values of declared variables that are not marked with `!own`! is undefined.`*

— Revised report on ALGOL 60, section "5. Declarations", p. 14

This definition is subtly different from a static variable: it only specifies behavior, and hence lifetime, not storage: an own variable can be allocated when a function is first called, for instance, rather than at program load time.

The use of the word `*static`* to refer to these variables dates at least to `F33f`_`[BCPL`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=BCPL]`_`f (1966), and has been popularized by the `F33f`_`[C programming language`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_programming_language]`_`f, which was heavily influenced by BCPL. The BCPL definition reads:

`*(1) Static data items: Those data items whose extents lasts as long as the program execution time; such data items have manifest constant Lvalues. Every static data item must have been declared either in a function or routine definition, in a global declaration or as a label set by colon.`*

— The BCPL Reference Manual, 7.2 Space Allocation and Extent of Data Items

Note that BCPL defined a "dynamic data item" for what is now called an `*automatic`* variable (local, stack-allocated), not for heap-allocated objects, which is the current use of the term `*dynamic allocation`*.

The `F33f`_`[static keyword`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Static_(keyword)]`_`f is used in C and related languages both for static variables and other concepts.

>>Addressing

The `F33f`_`[absolute address`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Absolute_address]`_`f `F33f`_`[addressing mode`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Addressing_mode]`_`f can only be used with static variables, because those are the only kinds of variables whose location is known by the compiler at compile time. When the program (`F33f`_`[executable`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Executable]`_`f or `F33f`_`[library`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Library_(computing)]`_`f) is `F33f`_`[loaded`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Loader_(computing)]`_`f into memory, static variables are stored in the `F33f`_`[data segment`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Data_segment]`_`f of the program's `F33f`_`[address space`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Address_space_(application_programming)]`_`f (if initialized), or the `F33f`_`[BSS segment`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=BSS_segment]`_`f (if uninitialized), and are stored in corresponding sections of `F33f`_`[object files`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Object_file]`_`f prior to loading.

>>Scope

In terms of `F33f`_`[scope and extent`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Variable_(computer_science)]`_`f, static variables have extent the entire run of the program, but may have more limited `F33f`_`[scope`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Scope_(computer_science)]`_`f. A basic distinction is between a `*static global variable`*, which has global scope and thus is in context throughout the program, and a `*`F33f`_`[static local variable`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Static_local_variable]`_`f,`* which has local scope. A static local variable is different from a local variable as a static local variable is initialized only once no matter how many times the function in which it resides is called and its value is retained and accessible through many calls to the function in which it is declared, e.g. to be used as a count variable. A static variable may also have `F33f`_`[module scope`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Module_scope]`_`f or some variant, such as `F33f`_`[internal linkage`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Internal_linkage]`_`f in `F33f`_`[C`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_programming_language]`_`f, which is a form of file scope or module scope.

>>>Example

An example of a static local variable in C:

`B100`F9d9#include <stdio.h>`f`b
`B100`F9d9`f`b
`B100`F9d9void Func() {`f`b
`B100`F9d9 static int x = 0;`f`b
`B100`F9d9 // |x| is initialized only once across five calls of |Func| and the variable`f`b
`B100`F9d9 // will get incremented five times after these calls. The final value of |x|`f`b
`B100`F9d9 // will be 5.`f`b
`B100`F9d9 x++;`f`b
`B100`F9d9 printf("%d\\n", x); // outputs the value of |x|`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9int main() {`f`b
`B100`F9d9 Func(); // prints 1`f`b
`B100`F9d9 Func(); // prints 2`f`b
`B100`F9d9 Func(); // prints 3`f`b
`B100`F9d9 Func(); // prints 4`f`b
`B100`F9d9 Func(); // prints 5`f`b
`B100`F9d9`f`b
`B100`F9d9 return 0;`f`b
`B100`F9d9}`f`b

>>Object-oriented programming

In `F33f`_`[object-oriented programming`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Object-oriented_programming]`_`f, there is also the concept of a `*`F33f`_`[static member variable`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Static_member_variable]`_`f`*, which is a "`F33f`_`[class variable`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Class_variable]`_`f" of a statically defined class, i.e., a `F33f`_`[member variable`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Member_variable]`_`f of a given class which is shared across all `F33f`_`[instances`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Class_instance]`_`f (objects), and is accessible as a member variable of these objects. A class variable of a dynamically defined class, in languages where classes can be defined at run time, is allocated when the class is defined and is not static.

Object constants known at compile-time, such as `F33f`_`[string literals`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=String_literal]`_`f, are usually allocated statically. In object-oriented programming, the `F33f`_`[virtual method tables`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Virtual_method_table]`_`f of classes are usually allocated statically. A statically defined value can also be `F33f`_`[global`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Global_variable]`_`f in its scope ensuring the same `F33f`_`[immutable`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Immutable_object]`_`f value is used throughout a run for consistency.

>>See also

• `F33f`_`[Constant (computer programming)`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Constant_(computer_programming)]`_`f
• `F33f`_`[Global variable`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Global_variable]`_`f
• `F33f`_`[Static method`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Static_method]`_`f
• `F33f`_`[Thread-local storage`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Thread-local_storage]`_`f

>>Notes

`:cite-note-1`!1.`! `F0af`_`[↑`#cite-ref-1]`_`f `:citerefjack-rons`aJack Rons. "What is static memory allocation and dynamic memory allocation?". MeritHub [An Institute of Career Development]. Archived from the original on June 11, 2010. Retrieved 2011-06-16. The compiler allocates required memory space for a declared variable. By using the addressof operator, the reserved address is obtained and this address may be assigned to a pointer variable. Since most of the declared variables have static memory, this way of assigning pointer value to a pointer variable is known as static memory allocation. Memory is assigned during compilation time.

>>References

• `:citerefkernighanritchie1988`a`F33f`_`[Kernighan, Brian W.`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Brian_Kernighan]`_`f; `F33f`_`[Ritchie, Dennis M.`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Dennis_Ritchie]`_`f (1988). `*The C Programming Language`* (2nd ed.). Upper Saddle River, NJ: Prentice Hall PTR. `F33f`_`[ISBN`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ISBN_(identifier)]`_`f 0-13-110362-8.
• `*`F33f`_`[The C++ Programming Language`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=The_C++_Programming_Language]`_`f`* (special edition) by `F33f`_`[Bjarne Stroustrup`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Bjarne_Stroustrup]`_`f (Addison Wesley, 2000; `F33f`_`[ISBN`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ISBN_(identifier)]`_`f 0-201-70073-5)

`c`F0af`_`[↑ Back to top`#top]`_`f`a